package com.mallcai.bigdata.ladon.service.report.impl.handler;

import com.mallcai.bigdata.ladon.client.vo.ReportDataVO;
import com.mallcai.bigdata.ladon.client.param.ReportQueryParam;
import com.mallcai.bigdata.ladon.datasource.RunInfoResult;
import com.mallcai.bigdata.ladon.service.report.impl.ReportQueryConstants;
import com.mallcai.bigdata.ladon.service.report.impl.ReportQueryContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Map;

/**
 * @author feixy
 * @version 1.0
 * @date 2019-07-20 13:06
 */
@Slf4j
@Service
@Order(700)
public class SetExtraInfoToResultPostHandler extends AbstractReportQueryPostHandler {

    @Override
    protected void postInternal(ReportQueryContext context) {
        RunInfoResult<ReportDataVO> result = context.getResult();
        Assert.notNull(result, "查询结果不能为空");
        ReportDataVO data = result.getData();
        if(result.isSuccess() && data != null){
            ReportQueryParam reportParam = context.getReportQueryParam();
            Map<String, Object> extraInfo = data.getExtraInfo();
            if(reportParam.isNeedRunSql()){
                extraInfo.put("sql", result.getSql());
            }
            extraInfo.put("cached", context.getInnerParam(ReportQueryConstants.KEY_IS_CACHE_RESULT));
            extraInfo.put("flowOff", context.getInnerParam(ReportQueryConstants.KEY_IS_FLOW_OFF));
        }
    }
}
